package cn.edu.scau.dormitory_management_system.controller;

import cn.edu.scau.dormitory_management_system.entity.DormRecord;
import cn.edu.scau.dormitory_management_system.service.DormRecordService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author djc
 * @date 2021/8/6
 * @time 10:37
 */
@RestController
@RequestMapping("/admin/dorm/record")
public class DormRecordController {
    @Autowired
    private DormRecordService recordService;

    @GetMapping("/list")
    public Object getRecord(@RequestParam(defaultValue = "1") int current,
                            int size) {
        JSONObject object = new JSONObject();

        QueryWrapper<DormRecord> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("record_id");
        IPage<DormRecord> page = new Page<>(current, size);
        page = recordService.page(page, wrapper);

        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }

        return object;
    }

    @GetMapping("/query")
    public Object getRecord(@RequestParam(defaultValue = "1") int current,
                            int size,
                            String stuName) {
        JSONObject object = new JSONObject();

        QueryWrapper<DormRecord> wrapper = new QueryWrapper<>();
        switch (stuName) {
            case "入宿":
                wrapper.eq("operation", 0);
                break;
            case "调宿":
                wrapper.eq("operation", 1);
                break;
            case "退宿":
                wrapper.eq("operation", 2);
                break;
            default:
                wrapper.like("stu_name", stuName).or().like("stu_id", stuName);
                break;
        }
        IPage<DormRecord> page = new Page<>(current, size);
        page = recordService.page(page, wrapper);

        if(page.getRecords().isEmpty()) {
            object.put("code", 0);
            object.put("msg", "无数据");
        }else {
            object.put("code", 200);
            object.put("page", page);
        }

        return object;
    }

    @PostMapping("/delete")
    public Object deleteRecord(int id) {
        JSONObject object = new JSONObject();

        boolean b = recordService.removeById(id);
        if(b) {
            object.put("code", 200);
        }else {
            object.put("code", 0);
        }

        return object;
    }

    @PostMapping("/deletes")
    public Object deleteRecord(@RequestBody List<Integer> ids) {
        JSONObject object = new JSONObject();

        boolean b = recordService.removeByIds(ids);

        if(b) {
            object.put("code", 200);
        }else {
            object.put("code", 0);
        }

        return object;
    }
}
